package org.csu.myPetStore.web.servlet;

import org.csu.myPetStore.domain.Account;
import org.csu.myPetStore.domain.Cart;
import org.csu.myPetStore.domain.Order;
import org.csu.myPetStore.service.CartService;
import org.csu.myPetStore.service.HotService;
import org.csu.myPetStore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Iterator;

public class ViewOrderServlet extends HttpServlet {

    private static final String VIEW_ORDER = "/WEB-INF/jsp/order/ViewOrder.jsp";

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        OrderService service = new OrderService();
        CartService cartService = new CartService();
        HotService hotService = new HotService();

        Account account = (Account) session.getAttribute("account");

        Order order = (Order) session.getAttribute("order");

        service.insertOrder(order);
        session.setAttribute("orderMsg","购买成功");
        session.setAttribute("cart",null);      //清空购物车
        hotService.updateGoodsByUser(account.getUsername());
        cartService.deleteAllItemByUsername(account.getUsername());
        session.setAttribute("order",order);
        req.getRequestDispatcher(VIEW_ORDER).forward(req,resp);

    }

}
